package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.About;
import com.hngm.bean.Message;
import com.hngm.bean.Product;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.MessageDao;

/**
 * Servlet implementation class MessageServlet
 */
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String url = request.getParameter("url");
		//创建留言MessageDao
		MessageDao messageDao = new MessageDao();
		if("addMsg".equals(url)){
			//添加留言
			//获取用户传递过来的留言信息
			String username = request.getParameter("username");
			String tel = request.getParameter("tel");
			String email = request.getParameter("email");
			String content = request.getParameter("content");
			
			//将用户的留言信息添加到数据库中
			//将留言信息封装成一个留言对象
			Message message = new Message();
			message.setName(username);
			message.setPhone(tel);
			message.setEmail(email);
			message.setContent(content);
			//调用DAO将留言信息插入到表中
			int result = messageDao.insertMsg(message);
			
			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "留言成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "留言失败"));
			}
		}else  if("queryMessageListByParam".equals(url)){
			//获取前端穿过来的查询条件
			String messagename = request.getParameter("messagename");
			String begintime = request.getParameter("begintime");
			String endtime = request.getParameter("endtime");
			String del = request.getParameter("del");
			String orderby = request.getParameter("orderby");
			//获取LayUI穿过来的分页信息
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = Integer.parseInt(request.getParameter("page"));
			
			//当前页开始的索引
			int beginIndex = (page - 1) * limit;
			
			//根据传过来得查询条件去查询产品列表
			ArrayList<Message> mList = messageDao.queryMessageListByParm(messagename,begintime,endtime,del,orderby,beginIndex,limit);
			
			//查询数据库中的数量
			int count = messageDao.queyrMessageListByParamCount(messagename,begintime,endtime,del,orderby);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, mList);
			out.print(str);
		}else if("add".equals(url)){
			//添加留言逻辑
			//1、获取用户有输入的产品信息
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String content = request.getParameter("content");
			Message p = new Message(name, phone, email, content);
			//2、将产品信息插入到表中（调用DAO）
			int result = MessageDao.insertMessage(p);
			//3、返回结果前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		}else if("del".equals(url)){
			//删除留言逻辑
			//1、获取前端传过来的产品Id
			String messageId = request.getParameter("messageId");
			//2、根据产品Id删除产品信息
			int result = messageDao.delMessage(messageId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		}else if("edit".equals(url)){
			//添加留言逻辑
			//1、获取用户有输入的产品信息
			long id = Long.parseLong(request.getParameter("id"));
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String content = request.getParameter("content");
			Message p = new Message(name, phone, email, content);
			p.setId(id);
			//2、将产品信息插入到表中（调用DAO）
			int result = MessageDao.edittMessage(p);
			//3、返回结果前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		
		}
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
